<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class contract extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
        $this->load->model('contract/contract_model');
        $this->load->model('member/member_model');
        $this->load->model('provider/provider_model');
        $this->load->model('category/category_model');
        $this->load->model('product/product_model');
    }

    public function index() {
    	$this->config->load('site', TRUE);
    	$this->_data['status'] = $this->config->item('status', 'site');
        $clients = $this->contract_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['contracts'] = $clients;
        $categorys = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = $categorys;
        $this->load->admintemplate('list', $this->_data);
    }

    public function search() {
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->_data['status'] = $this->config->item('status', 'site');
        $params['contract_name'] = $this->input->post('contract_name');
        $params['provider_id'] = $this->input->post('provider_id');
        $clients = $this->contract_model->getSearch($params, $sort = 'status', $order = 'asc');
        $this->_data['contracts'] = $clients;
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = $providers;
        $this->_data['contract_name'] = $params['contract_name'];
        $this->_data['provider_id'] = $params['provider_id'];
        $this->load->admintemplate('list', $this->_data);
    }
	function member($id){
		$this->config->load('form_validate', TRUE);
		$this->config->load('site', TRUE);
		$this->_data['status'] = $this->config->item('status', 'site');
		$params['member_id'] = $id;
		$params['contract_name'] = "";
		$params['provider_id'] = "";
		$clients = $this->contract_model->getSearch($params, $sort = 'status', $order = 'asc');
		$this->_data['contracts'] = $clients;
		$member = $this->member_model->getName($id);
		$this->_data['member'] = $member[0];
		$providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
		$this->_data['providers'] = $providers;
		$this->_data['id'] = $id;
		$this->load->admintemplate('member', $this->_data);
	}
    public function add($id) {
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->_data['status'] = $this->config->item('status', 'site');
        $this->_data['termination_date'] = $this->config->item('termination_date', 'site');
        $this->form_validation->set_rules($this->config->item('contract', 'form_validate'));
        $categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $categorys;
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = array();
        foreach ($providers as $key => $value) {
        	$this->_data['providers'][$value->id] = $value;
        }
        $this->_data['provider_ids'] = array();
        $this->_data['id'] = $id;
        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('add', $this->_data);
        } else {
        	$params['member_id'] = $id;
        	$params['contract_name'] = $this->input->post('contract_name', TRUE);
        	$params['contract_number'] = $this->input->post('contract_number', TRUE);
            $params['status'] = $this->input->post('status', TRUE);
            $params['category_id'] = $this->input->post('category_id', TRUE);
            $params['product_id'] = $this->input->post('product_id', TRUE);
            $params['provider_id'] = $this->input->post('provider_id', TRUE);
            $params['start_date'] = $this->input->post('start_date', TRUE);
            $params['end_date'] = $this->input->post('end_date', TRUE);
            $params['termination_alert'] = $this->input->post('termination_alert', TRUE);
            $params['termination_date'] = $this->input->post('termination_date', TRUE);
            $contract_id = $this->contract_model->insert($params);
            
            redirect('contract/member/'.$id);
        }
    }

    public function edit($member_id,$id) {
    	$this->config->load('site', TRUE);
    	$contract = $this->contract_model->find_id($id);
        $this->_data['contract'] = $contract;
        $this->_data['status'] = $this->config->item('status', 'site');
        $this->_data['termination_date'] = $this->config->item('termination_date', 'site');
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->form_validation->set_rules($this->config->item('contract', 'form_validate'));
        $categorys = $this->category_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['categorys'] = $categorys;
        $providers = $this->provider_model->find_all($sort = 'status', $order = 'asc');
        $this->_data['providers'] = $providers;
        $params['category_id'] = $contract->category_id;
        $params['provider_id'] = $contract->provider_id;
        $products = $this->product_model->getSearch($params);
        $this->_data['products'] = $products;
        $this->_data['member_id'] = $member_id;
        if ($this->form_validation->run() == FALSE) {
            $this->config->load('site', TRUE);
            $this->load->admintemplate('edit', $this->_data);
        } else {
        	$params['member_id'] = $member_id;
        	$params['contract_name'] = $this->input->post('contract_name', TRUE);
        	$params['contract_number'] = $this->input->post('contract_number', TRUE);
            $params['status'] = $this->input->post('status', TRUE);
            $params['category_id'] = $this->input->post('category_id', TRUE);
            $params['product_id'] = $this->input->post('product_id', TRUE);
            $params['provider_id'] = $this->input->post('provider_id', TRUE);
            $params['start_date'] = $this->input->post('start_date', TRUE);
            $params['end_date'] = $this->input->post('end_date', TRUE);
            $params['termination_alert'] = $this->input->post('termination_alert', TRUE);
            $params['termination_date'] = $this->input->post('termination_date', TRUE);
            $this->contract_model->update($id, $params);
            redirect('contract/member/'.$member_id);
        }
    }

    public function delete() {
        $id = $this->input->post('id');
        $this->db->trans_start();
        // Delete client
        $delete = (boolean) $this->provider_model->delete($id);
        if ($delete == false) {
            echo 0;
            $this->db->trans_complete();
            die;
        }
        // Delete client's config
        $this->client_config_model->delete($id);
        $this->db->trans_complete();
        if ($this->db->trans_status() !== false) {
            echo 1;
        } else {
            echo 0;
            $this->system_log->log('SQL Failed: ' . $this->db->last_query() . ' - ' . $this->db->_error_message());
        }
        die;
    }
	public function getproduct(){
		$params['category_id'] = $this->input->post('category_id', TRUE);
		$params['provider_id'] = $this->input->post('provider_id', TRUE);
		$products = $this->product_model->getSearch($params);
		$this->_data['products'] = $products;
		$this->load->view( 'contract/loadproduct', $this->_data);
	}
	public function getbymember(){
		$params['member_id'] = $this->input->post('member_id', TRUE);
		$contracts = $this->contract_model->getSearch($params);
		$this->_data['contracts'] = $contracts;
		$this->load->view( 'contract/loadcontract', $this->_data);
	}
}
